package com.xr.core.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xr.core.system.common.annotation.OperationLogAnno;
import com.xr.core.system.common.base.MyPage;
import com.xr.core.system.common.base.Result;
import com.xr.core.system.common.enums.OperationType;
import com.xr.core.system.common.groups.Save;
import com.xr.core.system.common.groups.Update;
import com.xr.core.system.entity.dictionaryModule.Dictionary;
import com.xr.core.system.service.DictionaryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 字典表 (Dictionary)表控制层
 */
@Validated
@RestController
@RequestMapping("/api/v1/dictionary")
public class DictionaryController {

    /**
     * 服务对象
     */
    @Resource
    private DictionaryService dictionaryService;

    /**
     * 通过父字典code 查询子集
     *
     * @param code 字典code
     * @return
     */
    @GetMapping("/listDicByParentCode")
    public Result<List<Dictionary>> listDicByParentCode(@NotBlank(message = "字典code不能为空") String code) {
        return Result.success(dictionaryService.listDicByParentCode(code));
    }

    /**
     * 查询所有数据
     *
     * @param dictionary 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryAll")
    public Result<List<Dictionary>> queryAll(Dictionary dictionary) {
        return Result.success(dictionaryService.list(new QueryWrapper<>(dictionary)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param dictionary 查询实体
     * @return 所有数据
     */
    @GetMapping("/queryByPage")
    public Result<MyPage<Dictionary>> queryByPage(MyPage<Dictionary> page, Dictionary dictionary) {
        return Result.success(dictionaryService.queryByPage(page, dictionary));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryOne/{id}")
    public Result<Dictionary> selectOne(@PathVariable Serializable id) {
        return Result.success(dictionaryService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param dictionary 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @OperationLogAnno(module = "字典模块", type = OperationType.INSERT, desc = "新增数据")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Dictionary dictionary) {
        return Result.isSuccess(dictionaryService.save(dictionary));
    }

    /**
     * 修改数据
     *
     * @param dictionary 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @OperationLogAnno(module = "字典模块", type = OperationType.UPDATE, desc = "修改数据")
    public Result<Object> update(@Validated(Update.class) @RequestBody Dictionary dictionary) {
        return Result.isSuccess(dictionaryService.updateById(dictionary));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @OperationLogAnno(module = "字典模块", type = OperationType.DELETE, desc = "删除单条数据")
    public Result<Object> delete(@PathVariable Long id) {
        Dictionary dictionary = new Dictionary();
        dictionary.setId(id);
        return Result.isSuccess(dictionaryService.removeById(dictionary));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/batchDelete")
    @OperationLogAnno(module = "字典模块", type = OperationType.DELETE, desc = "批量删除数据")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(dictionaryService.batchDelete(idList, new Dictionary()));
    }

}

